package com.ruoyi.web.controller.basice_info;

import com.ruoyi.basice_info.domain.TbExamRequirements;
import com.ruoyi.basice_info.service.ITbExamRequirementsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 考试要求目标设置Controller
 *
 * @author hongyi
 * @date 2023-12-08
 */
@RestController
@RequestMapping("/basice_info/requirements")
public class TbExamRequirementsController extends BaseController
{
    @Autowired
    private ITbExamRequirementsService tbExamRequirementsService;

    /**
     * 查询考试要求目标设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:list')")
    @GetMapping("/list")
    public TableDataInfo list(TbExamRequirements tbExamRequirements)
    {
        startPage();
        List<TbExamRequirements> list = tbExamRequirementsService.selectTbExamRequirementsList(tbExamRequirements);
        return getDataTable(list);
    }

    /**
     * 导出考试要求目标设置列表
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:export')")
    @Log(title = "考试要求目标设置", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbExamRequirements tbExamRequirements)
    {
        List<TbExamRequirements> list = tbExamRequirementsService.selectTbExamRequirementsList(tbExamRequirements);
        ExcelUtil<TbExamRequirements> util = new ExcelUtil<TbExamRequirements>(TbExamRequirements.class);
        util.exportExcel(response, list, "考试要求目标设置数据");
    }

    /**
     * 获取考试要求目标设置详细信息
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:query')")
    @GetMapping(value = "/{examRequirementsId}")
    public AjaxResult getInfo(@PathVariable("examRequirementsId") Long examRequirementsId)
    {
        return success(tbExamRequirementsService.selectTbExamRequirementsByExamRequirementsId(examRequirementsId));
    }

    /**
     * 新增考试要求目标设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:add')")
    @Log(title = "考试要求目标设置", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbExamRequirements tbExamRequirements)
    {
         addOrEdit(tbExamRequirements,"add");
        return toAjax(tbExamRequirementsService.insertTbExamRequirements(tbExamRequirements));
    }

    /**
     * 修改考试要求目标设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:edit')")
    @Log(title = "考试要求目标设置", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbExamRequirements tbExamRequirements)
    {
        addOrEdit(tbExamRequirements,"edit");
        return toAjax(tbExamRequirementsService.updateTbExamRequirements(tbExamRequirements));
    }


    public void addOrEdit(TbExamRequirements tbExamRequirements, String type) {
        TbExamRequirements requirementsCode = tbExamRequirementsService.getTbExamRequirementsByExamRequirementsCode(tbExamRequirements.getExamRequirementsCode());
        TbExamRequirements requirementsTarget = tbExamRequirementsService.getTbExamRequirementsByExamRequirementsTarget(tbExamRequirements.getExamRequirementsTarget());
        if(requirementsCode!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("考试要求代码已经存在");
            } else {
                if(!tbExamRequirements.getExamRequirementsId().equals(requirementsCode.getExamRequirementsId())) {
                    throw new IllegalArgumentException("考试要求代码已经存在");
                }
            }
        }
        if(requirementsTarget!=null) {
            if(type.equals("add")) {
                throw new IllegalArgumentException("考试要求目标已经已经存在");
            } else {
                if(!tbExamRequirements.getExamRequirementsId().equals(requirementsTarget.getExamRequirementsId())) {
                    throw new IllegalArgumentException("考试要求目标已经已经存在");
                }
            }
        }
    }
    /**
     * 删除考试要求目标设置
     */
    @PreAuthorize("@ss.hasPermi('basice_info:requirements:remove')")
    @Log(title = "考试要求目标设置", businessType = BusinessType.DELETE)
	@DeleteMapping("/{examRequirementsIds}")
    public AjaxResult remove(@PathVariable Long[] examRequirementsIds)
    {
        return toAjax(tbExamRequirementsService.deleteTbExamRequirementsByExamRequirementsIds(examRequirementsIds));
    }
}
